//
//  ViewController.m
//  UseBITCocoaLumberjack
//
//  Created by 郏国上 on 2018/12/18.
//  Copyright © 2018年 郏国上. All rights reserved.
//

#import "ViewController.h"
//判断字符串是否为空
#define isEmptyString(str) ([str isKindOfClass:[NSNull class]] || str == nil || ![str isKindOfClass:[NSString class]] || [str length] < 1)

@interface ViewController ()
@property (nonatomic,strong) UIButton *sendCodeBtn;
@property(nonatomic,strong) NSString *threadSno;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.sendCodeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.sendCodeBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.sendCodeBtn.backgroundColor = [UIColor greenColor];
    [self.sendCodeBtn setTitle:@"60秒" forState:UIControlStateNormal];
    self.sendCodeBtn.frame  = CGRectMake(50, 300, 100, 50);
    [self.view addSubview:self.sendCodeBtn];
    [self.sendCodeBtn addTarget:self action:@selector(sendCodeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    //这个加入主线程的函数调用时，由于它一直独占cpu并且由于程序再sleep所以无法刷新UI，所以点击按钮无效。
    [self test];
}

- (void)sendCodeBtnAction:(UIButton *)sender {
    [self testCode];
}

//测试加入GCD的MainDispatchQueue队列里的线程在后台仍旧可以运行三分钟
-(void)test
{
    FLDDLogDebug(@"测试加入GCD的MainDispatchQueue队列线程在后台仍旧可以运行");
    dispatch_async(dispatch_get_main_queue(), ^{
        for(NSInteger i = 0; ; i++)
        {
            sleep(1);
            FLDDLogDebug(@"test:%ld", (long)i);
            //注意：由于它已经在主线程了，不能使用dispatch_sync(dispatch_get_main_queue(), ^{回主线程刷线UI否则崩溃。就是这种起线程的方式只能刷新一次UI
        }
    });
}

//测试加入GCD的MainDispatchQueue队列里的线程在后台仍旧可以运行三分钟
-(void)testCode
{
    FLDDLogDebug(@"测试加入GCD的MainDispatchQueue队列线程在后台仍旧可以运行");
    [self.sendCodeBtn setTitle:@"60秒" forState:UIControlStateNormal];
    @weakify(self);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        @strongify(self);
        
        for(NSInteger i = 59; i >= -1 ; i--)
        {
            if(59 == i)
            {
                self.threadSno = [self getSocketThreadSno];
            }
            else
            {
                if(!isEmptyString(self.threadSno) && ![self.threadSno isEqualToString:[self getSocketThreadSno]])
                {
                    return ;
                }
            }
            FLDDLogDebug(@"test:%ld", (long)i);
            if(i >= 0)
            {
                dispatch_sync(dispatch_get_main_queue(), ^{
                    @strongify(self);
                    [self.sendCodeBtn setTitle:[NSString stringWithFormat:@"%ld秒", i] forState:UIControlStateNormal];
                });
                sleep(1);
            }
            else if(i == -1)
            {
                dispatch_sync(dispatch_get_main_queue(), ^{
                    @strongify(self);
                    [self.sendCodeBtn setTitle:@"60秒" forState:UIControlStateNormal];
                });
                usleep(100000);
            }
            else
            {
//                dispatch_sync(dispatch_get_main_queue(), ^{
//                    @strongify(self);
//                    [self.sendCodeBtn setTitle:@"60秒" forState:UIControlStateNormal];
//                });
               
                return;
            }
        }
    });
}

-(NSString *)getSocketThreadSno
{
    NSString * socketThreadDescription = [NSThread currentThread].description;
    if(socketThreadDescription.length <= 0)
    {
        return nil;
    }
    NSRange range = [socketThreadDescription rangeOfString:@"number = "];
    //    FLDDLogVerbose(@"socketThreadDescription:%@", socketThreadDescription);
    if(range.length > 0)
    {
        NSRange range1 = [socketThreadDescription rangeOfString:@","];
        if((range.length <= 0) || ((range1.length <= 0)))
        {
            return nil;
        }
        else
        {
            range = NSMakeRange (range.location + range.length, range1.location - range.location - range.length);
            if((range1.location > socketThreadDescription.length) || (range.location + range.length > socketThreadDescription.length))
            {
                return nil;
            }
            NSString *number = [socketThreadDescription substringWithRange : range];
            return number;
        }
    }
    else
    {
        //兼容ios8.0以后版本
        NSRange range = [socketThreadDescription rangeOfString:@"num = "];
        NSRange range1 = [socketThreadDescription rangeOfString:@"}"];
        if((range.length <= 0) || ((range1.length <= 0)))
        {
            return nil;
        }
        else
        {
            range = NSMakeRange (range.location + range.length, range1.location - range.location - range.length);
            if((range1.location > socketThreadDescription.length) || (range.location + range.length > socketThreadDescription.length))
            {
                return nil;
            }
            NSString *number = [socketThreadDescription substringWithRange : range];
            return number;
        }
    }
}

-(void)storeThreadSno
{
    NSString *threadSno = [self getSocketThreadSno];
    self.threadSno = threadSno;
}
@end
